package com.west.xu.redis.controller;

import com.google.gson.Gson;
import com.west.xu.redis.service.IRedisServie;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class RedisController {

    private static final Logger LOG = LoggerFactory.getLogger(RedisController.class);

    @Autowired
    private IRedisServie redisServie;

    @RequestMapping(value = "/cacheMsg/{msg}", method = RequestMethod.GET)
    @ApiOperation(value = "Redis服务", notes = "缓存数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "msg", value = "待缓存数据", dataType = "String", required = true)
    })
    public String cacheMsg(@PathVariable(name = "msg") String msg) {
        redisServie.cacheMsg(msg);
        LOG.info("Redis Cache Message ===>" + msg);
        return "Success";
    }

    @RequestMapping(value = "/fetchMsg", method = RequestMethod.GET)
    @ApiOperation(value = "Redis服务", notes = "获取缓存数据")
    public String fetchMsg() {
        String cacheMsg = redisServie.fetchMsg();
        LOG.info("Redis Fetch Message ===>" + cacheMsg);
        return cacheMsg;
    }

    @RequestMapping(value = "/batchCache", method = RequestMethod.GET)
    @ApiOperation(value = "Redis", notes = "批量缓存数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "count", example = "100", value = "待缓存数据个数", dataType = "Long", required = true)
    })
    public String batchCache(@RequestParam(value = "count") Integer count) {
        List<String> dataList = new ArrayList<>();
        for (int n = 0; n < count; n++) {
            dataList.add(RandomStringUtils.randomAlphanumeric(20));
        }
        List<Object> result = redisServie.cacheBatchData(dataList);
        String res = new Gson().toJson(result);
        LOG.info("Redis Cache Result [" + result.size() + "]===>" + res);
        return res;
    }

    @RequestMapping(value = "/batchFetch", method = RequestMethod.GET)
    @ApiOperation(value = "Redis", notes = "批量获取数据")
    public String batchFetch() {
        Map map = redisServie.fetchDataMap();
        String res = new Gson().toJson(map);
        LOG.info("Redis Cache Result ===>" + res);
        return res;
    }

    @RequestMapping(value = "/publish/{channel}/{msg}", method = RequestMethod.GET)
    @ApiOperation(value = "Redis服务", notes = "Redis发布订阅")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "channel", value = "发布通道", example = "channel_west", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "path", name = "msg", value = "待发布数据", dataType = "String", required = true)
    })
    public String publish(@PathVariable(name = "channel") String channel,
                          @PathVariable(name = "msg") String msg) {
        redisServie.publishMsg(channel, msg);
        LOG.info("Redis Publish Msg ===>" + msg);
        return "Success";
    }
}
